package com.privatbank.DTO;

/**
 * Created by Slava on 02.08.2014.
 */

import com.privatbank.constants.Constants;
import com.privatbank.tables.Good;
import com.privatbank.tables.Category;
import com.privatbank.tables.Manufacturer;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.sql.SQLException;

public class GoodDTO {
    private final String login;
    private final String password;
    private final String url;


    private String manufacturer;
    private String model;
    private String category;
    private double price;
    private String description;
    private String image;
    private String storage;



    public GoodDTO(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getCategory() {
        return category;
    }


    public List<GoodDTO> fullGood() throws SQLException {
        List<GoodDTO> goods = new ArrayList<GoodDTO>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            con = DriverManager.getConnection(url, login, password);

            stmt = con.prepareStatement("SELECT t1.model, t2.name_manufacturer, t3.name_category FROM Goods as t1 INNER JOIN manufacturer as t2 on t1.ID_manufacturer=t2.ID_manufacturer INNER JOIN category as t3 on t1.ID_category=t3.ID_category");
            rs = stmt.executeQuery();
            while (rs.next()) {
                GoodDTO dto = new GoodDTO(Constants.URL, Constants.LOGIN, Constants.PASSWORD);
                dto.setModel(rs.getString(1));
                dto.setManufacturer(rs.getString(2));
                dto.setCategory(rs.getString(3));
                goods.add(dto);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return goods;
    }
}
